package c.j.a.d.b.j;

import c.b.a.a.a;
import c.j.a.c.y.a.i;
import c.j.a.d.b.d.q;
import c.j.a.d.b.e.b;
import c.j.a.d.b.i.h;
import c.j.a.d.b.m.c;
import c.j.a.d.b.m.d;
import com.huawei.agconnect.exception.AGCServerException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;

/* compiled from: DefaultDownloadEngine */
public class j extends c {
    public static d k;

    public j() {
        k = new d();
    }

    public static Runnable t(List<Future> list) {
        if (!list.isEmpty()) {
            try {
                ExecutorService t = b.t();
                if (t instanceof ThreadPoolExecutor) {
                    BlockingQueue queue = ((ThreadPoolExecutor) t).getQueue();
                    if (!(queue == null || queue.isEmpty())) {
                        Object obj;
                        for (Future future : list) {
                            if ((future instanceof Runnable) && queue.remove(future)) {
                                obj = (Runnable) future;
                                break;
                            }
                        }
                        obj = null;
                        if (obj != null) {
                            list.remove(obj);
                            return obj;
                        }
                    }
                }
            } catch (Throwable th) {
                StringBuilder g = a.g("getUnstartedTask() error: ");
                g.append(th.toString());
                c.j.a.d.b.g.a.f("DefaultDownloadEngine", g.toString());
            }
        }
        return null;
    }

    public List<Integer> a() {
        ArrayList arrayList;
        d dVar = k;
        Objects.requireNonNull(dVar);
        synchronized (d.class) {
            dVar.a();
            arrayList = new ArrayList();
            for (int i = 0; i < dVar.a.size(); i++) {
                c cVar = (c) dVar.a.get(dVar.a.keyAt(i));
                if (cVar != null) {
                    arrayList.add(Integer.valueOf(cVar.E()));
                }
            }
        }
        return arrayList;
    }

    public void d(int i, long j) {
        d dVar = k;
        if (dVar != null) {
            c cVar = (c) dVar.a.get(i);
            if (cVar != null) {
                h hVar = cVar.x;
                if (hVar != null && (hVar instanceof c.j.a.d.b.i.b)) {
                    try {
                        c.j.a.d.b.i.b bVar = (c.j.a.d.b.i.b) hVar;
                    } catch (Throwable th) {
                        th.printStackTrace();
                    }
                }
            }
        }
    }

    public void e(int i, c.j.a.d.b.h.c cVar) {
        int i2;
        q qVar;
        com.ss.android.socialbase.downloader.e.a aVar;
        com.ss.android.socialbase.downloader.g.c cVar2;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("start doDownload for task : ");
        stringBuilder.append(i);
        c.j.a.d.b.g.a.d("DownloadTask", stringBuilder.toString());
        c cVar3 = new c(cVar, this.j);
        d dVar = k;
        Objects.requireNonNull(dVar);
        cVar3.G = System.currentTimeMillis();
        cVar3.u.a();
        synchronized (d.class) {
            int i3 = dVar.b;
            i2 = 0;
            if (i3 >= AGCServerException.UNKNOW_EXCEPTION) {
                dVar.a();
                dVar.b = 0;
            } else {
                dVar.b = i3 + 1;
            }
            dVar.a.put(cVar3.E(), cVar3);
        }
        cVar = cVar3.b;
        com.ss.android.socialbase.downloader.g.c cVar4;
        try {
            ExecutorService s = b.s();
            if (cVar != null) {
                cVar4 = cVar.a;
                if (cVar4 != null) {
                    if ("mime_type_plugin".equals(cVar4.u) && c.j.a.d.b.l.a.f.b("divide_plugin", 1) == 1) {
                        cVar.a.t("executor_group", Integer.valueOf(3));
                    }
                    int Z = cVar.a.Z();
                    if (Z == 3) {
                        s = b.r != null ? b.r : b.s();
                    } else if (Z == 4) {
                        s = b.s != null ? b.s : b.s();
                    }
                }
            }
            if (s == null) {
                q qVar2 = cVar.l;
                com.ss.android.socialbase.downloader.g.c cVar5 = cVar.a;
                com.ss.android.socialbase.downloader.e.a aVar2 = new com.ss.android.socialbase.downloader.e.a(1003, "execute failed cpu thread executor service is null");
                com.ss.android.socialbase.downloader.g.c cVar6 = cVar.a;
                i.n(qVar2, cVar5, aVar2, cVar6 != null ? cVar6.o0() : 0);
            } else if (c.j.a.d.b.l.a.d(cVar3.E()).k("pause_with_interrupt", false)) {
                cVar3.a = s.submit(cVar3);
            } else {
                s.execute(cVar3);
            }
        } catch (Exception e) {
            if (cVar != null) {
                qVar = cVar.l;
                cVar4 = cVar.a;
                aVar = new com.ss.android.socialbase.downloader.e.a(1003, c.j.a.d.b.n.a.B(e, "DownloadThreadPoolExecute"));
                cVar2 = cVar.a;
                if (cVar2 != null) {
                    i2 = cVar2.o0();
                }
                i.n(qVar, cVar4, aVar, i2);
            }
            e.printStackTrace();
        } catch (OutOfMemoryError e2) {
            if (cVar != null) {
                qVar = cVar.l;
                cVar4 = cVar.a;
                aVar = new com.ss.android.socialbase.downloader.e.a(1003, "execute OOM");
                cVar2 = cVar.a;
                if (cVar2 != null) {
                    i2 = cVar2.o0();
                }
                i.n(qVar, cVar4, aVar, i2);
            }
            e2.printStackTrace();
        }
    }

    public void i(c cVar) {
        d dVar = k;
        if (dVar != null) {
            synchronized (d.class) {
                try {
                    if (i.s(524288)) {
                        int indexOfValue = dVar.a.indexOfValue(cVar);
                        if (indexOfValue >= 0) {
                            dVar.a.removeAt(indexOfValue);
                        }
                    } else {
                        dVar.a.remove(cVar.E());
                    }
                } catch (Throwable th) {
                    th.printStackTrace();
                }
            }
        }
    }

    public boolean j(int i) {
        d dVar = k;
        if (dVar == null) {
            return false;
        }
        Object obj;
        Objects.requireNonNull(dVar);
        synchronized (d.class) {
            if (dVar.a != null) {
                if (dVar.a.size() > 0) {
                    c cVar = (c) dVar.a.get(i);
                    obj = (cVar == null || !cVar.l.get()) ? null : 1;
                }
            }
            obj = null;
        }
        if (obj == null) {
            return false;
        }
        com.ss.android.socialbase.downloader.g.c n = n(i);
        if (n == null) {
            return false;
        }
        if (i.x(n.o0())) {
            return true;
        }
        k(i);
        return false;
    }

    public void k(int i) {
        d dVar = k;
        if (dVar != null) {
            Objects.requireNonNull(dVar);
            synchronized (d.class) {
                dVar.a();
                c cVar = (c) dVar.a.get(i);
                if (cVar != null) {
                    cVar.l();
                    dVar.b(cVar);
                    dVar.a.remove(i);
                }
            }
        }
    }

    public c m(int i) {
        d dVar = k;
        c cVar = null;
        if (dVar == null) {
            return null;
        }
        Objects.requireNonNull(dVar);
        synchronized (d.class) {
            dVar.a();
            c cVar2 = (c) dVar.a.get(i);
            if (cVar2 != null) {
                c.j.a.d.b.b.i iVar = c.j.a.d.b.b.i.RUN_STATUS_CANCELED;
                cVar2.m = iVar;
                if (cVar2.f != null) {
                    cVar2.f.f();
                } else {
                    cVar2.I();
                    cVar2.m = iVar;
                    cVar2.H();
                }
                cVar2.e();
                dVar.b(cVar2);
                dVar.a.remove(i);
                cVar = cVar2;
            }
        }
        return cVar;
    }
}
